home *** CD-ROM | disk | FTP | other *** search
/ MIDICraft's MIDINET CD-ROM / MIDICraft's MIDINET CD-ROM.iso / DOSUTILS / KORGI3 / RECVSYX.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-03  |  3.3 KB  |  184 lines

  1. // recvsyx: record data from midi input
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include "sb.hpp"
  8. #include "filei3.hpp"
  9.  
  10. char filename[128] = "recv.syx";
  11. Soundcard* card = 0;
  12. FILE* f = 0;
  13.  
  14. int hex = 0;
  15. int sysex_only = 0;
  16. int echo = 0;
  17.  
  18. unsigned char buf[32000];
  19. int buflen = 0;
  20. long skip = 0;
  21.  
  22. void flushbuf()
  23. {
  24.   if (buflen > 0)
  25.   {
  26.     if (hex)
  27.     {
  28.     static int col = 0;
  29.  
  30.       for (int i = 0; i < buflen; i++)
  31.       {
  32.     if (col >= 25 || (buf[i] >= 0x80 && buf[i] != 0xf8 && buf[i] != 0xfe && buf[i] != 0xf7))
  33.     {
  34.       fputc('\r', f);
  35.       fputc('\n', f);
  36.       col = 0;
  37.     }
  38.     fprintf(f, "%02X ", buf[i]);
  39.     col++;
  40.       }
  41.     }
  42.     else
  43.     {
  44.       if (fwrite(buf, buflen, 1, f) != 1)
  45.     fprintf(stderr, "write error\n");
  46.     }
  47.   }
  48.  
  49.   buflen = 0;
  50. }
  51.  
  52. void add(FILE* f, unsigned char* s, int len)
  53. {
  54.   if (buflen + 7 > sizeof(buf))
  55.     flushbuf();
  56.   memcpy(buf+buflen, s, len);
  57.   if (len > 0)
  58.     buflen += len;
  59. }
  60.  
  61. int receive_syx()
  62. {
  63.   card->reset();
  64.   unsigned char c;
  65.   int ret = 0;
  66.  
  67.   fprintf(stderr, "recording...\n");
  68.   card->startinput();
  69.  
  70.   f = fopen(filename, "wb");
  71.   if (!f)
  72.   {
  73.     perror(filename);
  74.     return 0;
  75.   }
  76.   int online = 0;
  77.   int insysex = 0;
  78.  
  79.   while (!kbhit() || getch() != 27)
  80.   {
  81.     int count = card->hear(&c, 1);
  82.     if (count <= 0)
  83.       continue;
  84.     if (c == 0xf8 || c == 0xfe)
  85.     {
  86.       if (online == 0)
  87.     fprintf(stderr,"*\b");
  88.       else if (online == 12)
  89.     fprintf(stderr,".\b");
  90.       online++;
  91.       if (online == 24)
  92.     online = 0;
  93.     }
  94.     if (insysex)
  95.     {
  96.       if (c == 0xf7)
  97.       {
  98.     insysex = 0;
  99.       }
  100.     }
  101.     else
  102.     {
  103.       if (c == 0xf0)
  104.       {
  105.     insysex = 1;
  106.       }
  107.       else if (sysex_only)
  108.     continue;
  109.     }
  110.  
  111.     add(f, &c, 1);
  112.     if (echo)
  113.     {
  114.       printf(" %02X", c);
  115.       if (c == 0xF7)
  116.     putchar('\n');
  117.     }
  118.     ret = 1;
  119.   }
  120.   if (echo)
  121.     printf("\n");
  122.   card->stopinput();
  123.   if (f)
  124.   {
  125.     flushbuf();
  126.     fclose(f); f = 0;
  127.   }
  128.   return ret;
  129. }
  130.  
  131. int main(int argc, char** argv)
  132. {
  133.   argc--; argv++;
  134.   while (argc > 0 && **argv == '-')
  135.   {
  136.     if (strnicmp(*argv, "-hex", 4) == 0)
  137.     {
  138.       hex = 1;
  139.       argc--; argv++;
  140.       continue;
  141.     }
  142.     if (strnicmp(*argv, "-h", 2) == 0)
  143.     {
  144.       printf("usage: recvsyx [-h] [-hex] [-echo] [-sysex] [filename]\n");
  145.       printf("-hex\tsave received data to text file (default: binary file)\n");
  146.       printf("-echo\techo the received data (hexadecimal)\n");
  147.       printf("-sysex\tsave only sysex messages (F0 ... F7)\n");
  148.       printf("filename\tsave data to filename (default: recv.syx)\n");
  149.       return 1;
  150.     }
  151.     if (strnicmp(*argv, "-echo", 2) == 0)
  152.     {
  153.       echo = 1;
  154.       argc--; argv++;
  155.       continue;
  156.     }
  157.     if (strnicmp(*argv, "-sysex", 2) == 0)
  158.     {
  159.       sysex_only = 1;
  160.       argc--; argv++;
  161.       continue;
  162.     }
  163.     fprintf(stderr, "invalid option %s\n", *argv);
  164.     argc--; argv++;
  165.   }
  166.   if (argc > 0)
  167.   {
  168.     strcpy(filename, *argv);
  169.   }
  170.   card = detect_soundcard();
  171.   if (!card)
  172.   {
  173.     fprintf(stderr, "Could not detect soundcard\n");
  174.     return 1;
  175.   }
  176.  
  177.   if (!receive_syx())
  178.     fprintf(stderr, "no response from soundcard\n");
  179.   else
  180.     fprintf(stderr, "data written to file %s\n", filename);
  181.   delete card;
  182.   return 0;
  183. }
  184.